function FLogCountSquads () {
    var server = top.FLogGetServer ();
    var facility = server.GetStarbase (window.userloc, "squadrons");
    facility["sector"] = POgetSector ();
    
    var label = document.getElementsByTagName ("h1")[0];
    
    var elem = label;
    var squads = {};
    for (var i=0; i<label.parentNode.childNodes.length; i++) {
        elem = elem.nextSibling;
        if (elem == null) break;
        if (elem.tagName.toUpperCase() == "TABLE") {
            var b = elem.getElementsByTagName ("B")[0].textContent;
            if (squads[b] == null) {
                squads[b] = 1;
            } else {
                squads[b] += 1;
            }
        }
    }
    
    var desc = "";
    var first = true;
    for (var sname in squads) {
        var sval = squads[sname];
        if (first) first = false;
        else {
            desc += ", ";
        }
        desc += sname + "(" + sval + "x)";
    }
    
    facility["squadrons"] = desc;
    
    server.SaveFacility (window.userloc, facility, "squadrons");
}

FLogCountSquads ();
FLogCompactScreen ();


///////////////////////////////////////////
// Compact Squads screen
///////////////////////////////////////////
function FLogCompactScreen () {
    function outerHTML(node){
            return node.outerHTML || new XMLSerializer().serializeToString(node);
    }
    
    function getColor(squadSize) {
            // if size < 50 then red, if < 80 then yellow
            if (squadSize < 50) {
                    return 'red'
            } else {
                    if (squadSize < 80) {
                            return 'yellow'
                    }
                    else {
                            return 'white'
                    }
            }
    }
    
    function makeTable(rowCount, cellCount) {
    
            row = new Array();
            cell = new Array();
            tab = document.createElement('table');
            tab.setAttribute('id', 'squads');
    
            for (c = 0; c < rowCount; c++) {
            
                    row[c] = document.createElement('tr');
                    for (k = 0; k < cellCount; k++) {
                            cell[k] = document.createElement('td');
                            cell[k].align = 'center';
                            cell[k].width = '100px';				
                            row[c].appendChild(cell[k]);
                    }
                    tab.appendChild(row[c]);
            }
            
            return tab;
    }
    
    var tblsStart = 3;
    var tblWidth = 6;
    var squads = makeTable(8, tblWidth);
    squads.align = 'center';
    squads.rows[0].cells[0].appendChild(document.createElement('br'));
    squads.rows[7].cells[0].appendChild(document.createElement('br'));
    
    var tables = document.getElementsByTagName('table');
    if (!tables[tblsStart]) return;	// in case no squads at all
    
    for(i = tblsStart; i < tables.length; i++)
    {
            var aTable = tables[i];
            var bolds = aTable.getElementsByTagName('b');
            var images = aTable.getElementsByTagName('img');
            var links = aTable.getElementsByTagName('a');
    
            // remove BR tags between squad tables
            aTable.parentNode.removeChild(aTable.nextSibling);
            aTable.parentNode.removeChild(aTable.nextSibling);
            
            var rowOffset = Math.abs(Math.floor((i - tblsStart) / tblWidth));
            var cellNo = (i - tblsStart) - (rowOffset * 6);
            rowOffset = rowOffset * 3;
            squads.rows[1 + rowOffset].cells[cellNo].appendChild(links[0].cloneNode(true));
            squads.rows[1 + rowOffset].cells[cellNo].firstChild.innerHTML = 'Hire ' + bolds[0].innerHTML.split(' ', 1)[0];
            squads.rows[1 + rowOffset].cells[cellNo].firstChild.setAttribute('style', 'color:' + getColor(bolds[0].innerHTML.split(' ', 1)[0]));
            squads.rows[2 + rowOffset].cells[cellNo].appendChild(images[0].cloneNode(true));
            squads.rows[2 + rowOffset].cells[cellNo].firstChild.removeAttribute('align');
            squads.rows[3 + rowOffset].cells[cellNo].innerHTML = bolds[1].innerHTML + '<br>' + bolds[2].innerHTML + ' ' + outerHTML(images[2]) + '<br><br><br>';
            
    }
    
    // apply styling
    squads.style.cssText = tables[tblsStart].style.cssText + ';font-weight: bold;';
    squads.setAttribute('class', 'messagestyle');
    
    // remove original squads
    for(i = tables.length - 1; i > 2; i--) {
            tables[i].parentNode.removeChild(tables[i]);
    }
    
    // add new squads table
    tables[2].appendChild(squads);
    tables[2].appendChild(document.createElement('br'));
    tables[2].appendChild(document.createElement('br'));
    
    // add direct link to trade screen (for faster hw buying :P)
    var trade = document.createElement('a');
    trade.href = 'http://orion.pardus.at/starbase_trade.php';
    trade.innerHTML = '<b><center>Jump to starbase trade screen...</center></b><br>';
    tables[2].appendChild(trade);

}